import sun.reflect.generics.tree.Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 牛孟俊
 * Date: 2023-11-02
 * Time: 22:38
 */
public class BinarySearchTree {

    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

    }

    public TreeNode root;//根节点

    /**
     * 查找功能
     * 时间复杂度: 是一棵完全二叉树（或者是一个满二叉树） O(logN)  坏的情况，只有一个左子树或者右子树O(n)
     * 
     *
     * @param val 所要查找的值
     * @return 返回val节点的地址    没找到返回null
     */
    public TreeNode search(int val) {
        TreeNode cur = root;

        //cur不为空，才可能找到
        while(cur != null) {
            if(val < cur.val) {
                cur = cur.left;
            } else if(val > cur.val) {
                cur = cur.right;
            } else {
                return cur;
            }
        }
        //走到这一步说明cur == null  则没找到
        return null;

    }

    /**
     * 插入功能
     * @param key 插入的元素
     * @return
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);
        if(root == null) {
            root = node;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        if(cur == null) {
            cur.val = key;
        }
        while(cur != null) {
            if(key < cur.val) {
                parent = cur;
                cur = cur.left;
            } else if(key > cur.val){
                parent = cur;
                cur = cur.right;
            } else {
                return false; //不能插入相同的值
            }
        }
        if(parent.val < key) {
            parent.right = node;
        } else {
            parent.left = node;
        }
        return true;
    }

    /**
     * 找到要删除key的节点位置
     * @param key
     */
    public void removeNode(int key) {
        if(root == null) {
            return;//空树
        }
        TreeNode cur = root;
        TreeNode parent = null;//记录cur的根节点

        while(cur != null) {
            if(cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                remove(cur,parent);//ALT + 回车 快速生成这个方法 ，
                return;
            }
        }
    }

    /**
     * 删除这个cur节点
     * @param cur 要删除的节点
     * @param parent 要删除节点的父节点
     */
    private void remove(TreeNode cur, TreeNode parent) {
        if(cur.left == null) {
            //cur没有左树
            if(cur == root) {
                root = parent.right;
            } else if(cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }

        } else if(cur.right == null) {
            //cur没有右树
            if(cur == root) {
                root = parent.left;
            } else if(cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {
            //cur左树和右树都存在
            //去右边找
            TreeNode target = cur.right;
            TreeNode targetParent = cur  ;
            while(target.left != null) {
                targetParent = target;
                target = target.left;
            }

            cur.val = target.val;
            if(target == targetParent.left) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;

                
            }
        }
    }

}
